package com.bw.livepush;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioFormat;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.frank.live.LivePusherNew;
import com.frank.live.camera2.Camera2Helper;
import com.frank.live.listener.LiveStateChangeListener;
import com.frank.live.param.AudioParam;
import com.frank.live.param.VideoParam;
import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;

import org.jetbrains.annotations.NotNull;

public class LivePushActivity extends AppCompatActivity implements MsgCallBack,
        View.OnClickListener, LiveStateChangeListener {

    private WebSocketService socketService;
    private ConnectionBind connectionBind;
    private SVGAImageView svgImg;
    private SVGAParser svgaParser;
    private SurfaceView live_surface;
    private LivePusherNew livePush;
    private int width = 640;
    private int height = 480;
    private int videoBitRate =800000;
    private int videoFrameRate = 10;
    private VideoParam videoParam;
    private int sampleRate = 44100;
    private int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    private int numChannels = 2;
    private AudioParam audioParam;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_livepush);
        init();
//        startService(new Intent(this,WebSocketService.class));
//        connectionBind = new ConnectionBind();
//        bindService(new Intent(this,WebSocketService.class),connectionBind,BIND_AUTO_CREATE);
//        svgImg = findViewById(R.id.svgImg);
//        svgImg.setLoops(1);
//        svgImg.setCallback(new SVGACallback() {
//            @Override
//            public void onPause() {
//
//            }
//
//            @Override
//            public void onFinished() {
//
//            }
//
//            @Override
//            public void onRepeat() {
//
//            }
//
//            @Override
//            public void onStep(int i, double v) {
//
//            }
//        });
//        svgaParser = SVGAParser.Companion.shareParser();
//        svgaParser.init(this);
//        loadAnimation();
    }

    private void init(){
        live_surface = findViewById(R.id.live_surface);
        findViewById(R.id.push_stream_btn).setOnClickListener(this);
        initPush();
    }

    private void initPush(){
        videoParam = new VideoParam(width,height,
                Integer.valueOf(Camera2Helper.CAMERA_ID_FRONT),videoBitRate,videoFrameRate);
        audioParam = new AudioParam(sampleRate,channelConfig,audioFormat,numChannels);
        livePush = new LivePusherNew(this,videoParam,audioParam);
        livePush.setPreviewDisplay(live_surface.getHolder());
//        livePush.start(LiveConst.LIVE_URL);
    }


    private void loadAnimation(){
        String name = "rose.svga";
        svgaParser.decodeFromAssets(name, new SVGAParser.ParseCompletion() {
            @Override
            public void onComplete(@NotNull SVGAVideoEntity svgaVideoEntity) {
                svgImg.setVideoItem(svgaVideoEntity);
                svgImg.stepToFrame(0, true);
            }

            @Override
            public void onError() {

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        livePush.stopPush();
        livePush.release();
//        unbindService(connectionBind);
//        stopService(new Intent(this,WebSocketService.class));
    }

    @Override
    public void onClick(View v) {
        livePush.startPush(LiveConst.LIVE_URL,this);
    }

    @Override
    public void onError(String msg) {
        Log.e("ZXY","push->erro:"+msg);
    }

    private class ConnectionBind implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            WebSocketService.WebSocketBinder webSocketBinder = (WebSocketService.WebSocketBinder) iBinder;
            socketService = webSocketBinder.getWebSocketService();
            socketService.setCallBack(LivePushActivity.this::msgCall);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }

    @Override
    public void msgCall(MsgEntity msgEntity) {
        if (msgEntity instanceof GiftMsgEntity){
            loadAnimation();
        }
    }
}
